package com.xiaoshou.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xiaoshou.pojo.Files;
import com.xiaoshou.pojo.Question;
import com.xiaoshou.pojo.QuestionFilesRel;
import com.xiaoshou.pojo.User;
import com.xiaoshou.util.CreateTime;
import com.xiaoshou.util.FileUtil;
import com.xiaoshou.util.MyUUID;
import com.xiaoshou.util.StringUtils;

@Service
@Transactional
public class FilesService extends BaseService {

	public void saveFilesOfUpload(User user, String questionId, File file, Files files) {
		String nowTime = CreateTime.createTime();
		Question q = getQuestionDao().findById(questionId);

		Files f = new Files();
		f.setFilename(StringUtils.replaceHtml(files.getFilename()));
		f.setFilesize(FileUtil.getFileSize(file.length()));
		f.setUser(user);
		f.setFileurl(files.getFileurl());
		f.setId(MyUUID.createUUID().toString());
		f.setUploadtime(nowTime);
		f.setLastupdatetime(nowTime);
		try {
			f.setMd5(DigestUtils.md5Hex(new FileInputStream(file.getPath())));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(q.getQuestiontype().getId());
		getFilesDao().saveFileOfQuestion(f, q);
	}

	public List<String> getMd5List() {
		return getFilesDao().getFileMd5List();

	}

	public void delFileInTomcat(QuestionFilesRel questionFileRel) {
		String fileUrl = questionFileRel.getFiles().getFileurl();
		File file = new File(fileUrl);
		if (file.isFile() && file != null) {
			file.delete();
		} else {
			SimpleEmail email = new SimpleEmail();
			email.setHostName("SMTP.126.COM");// 发送方
			email.setAuthentication("bioroadlab@126.com", "admin0123456789");// 发件人
			email.setCharset("UTF-8");
			email.setTLS(true);
			try {
				email.setFrom("bioroadlab@126.com", "admin");// 发件人
				email.setSubject("发送主题");
				email.setMsg("删除tomcat中存的文件出错！---" + CreateTime.createTime());
				email.addTo("wei_lucky@126.com".toString());// 收件人
				email.send();
			} catch (EmailException e) {
				e.printStackTrace();
			} finally {
				System.out.println("finally语句!!!");
			}
			System.out.println("邮件发送成功！");
		}
	}

	public void updateFilesName(String filename, String fileId) {
		Files f = getFilesDao().findById(fileId);
		System.out.println(f.getUser().getId());
		f.setFilename(filename);
		f.setLastupdatetime(CreateTime.createTime());
		String fileUrl = f.getFileurl();
		File fileInTomcat = new File(fileUrl);
		System.out.println(f.getId());
		System.out.println(f.getFileurl());
		System.out.println(f.getUser().getId());
		System.out.println(f.getFilename());
		System.out.println(fileUrl);// suffix prefix

		String prefix = fileUrl
				.substring(0, fileUrl.lastIndexOf("files\\") + 6);
		String suffix = fileUrl.substring(fileUrl.lastIndexOf("."),
				fileUrl.length());
		System.out.println(prefix + "\t" + suffix);
		String newUrl = prefix + filename + suffix;
		// 修改硬盘上文件的名字
		fileInTomcat.renameTo(new File(newUrl));

		f.setFileurl(newUrl);
		getFilesDao().updateFiles(f);

	}

	public boolean judgeUrl(Files f) {
		String suffix = f.getFileurl().substring(
				f.getFileurl().lastIndexOf(".") + 1, f.getFileurl().length());
		List<String> suffixList = new ArrayList<String>();
		suffixList.add("doc");
		suffixList.add("docx");
		suffixList.add("ppt");
		suffixList.add("pptx");
		suffixList.add("xlsx");
		suffixList.add("xls");
		suffixList.add("txt");
		suffixList.add("png");
		suffixList.add("jpg");
		suffixList.add("gif");
		suffixList.add("swf");
		suffixList.add("pdf");
		suffixList.add("mp3");
		suffixList.add("html");
		suffixList.add("htm");
		suffixList.add("xml");
		if (suffixList.contains(suffix)) {
			return true;
		} else {
			return false;
		}
	}

	public void delFilesOfQuestion(Files files) {
		getFilesDao().delFileInRel(files);// 删除关系表中的数据
		getFilesDao().del(files);
	}

	public void deleteFileInQuestion(String fileId) {
		Files f = getFilesDao().findById(fileId);
		// 删除磁盘上的文件
		File file = new File(f.getFileurl());
		file.delete();

		getFilesDao().deleteFileInPage(f);// 删除库中的数据

	}

}
